package com.atguigu.spzx.manager.service.Impl;

import com.atguigu.spzx.manager.mapper.SysRoleMenuMapper;
import com.atguigu.spzx.manager.service.SysMenuService;
import com.atguigu.spzx.manager.service.SysRoleMenuService;
import com.atguigu.spzx.model.dto.system.AssginMenuDto;
import com.atguigu.spzx.model.entity.system.SysMenu;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * ClassName: SysRoleMenuServiceImpl
 * Package: com.atguigu.spzx.manager.service.Impl
 * Description:
 *
 * @Author 刘强
 * @Create 2024/7/3 16:33
 * @Version 1.0
 */
@Service
public class SysRoleMenuServiceImpl implements SysRoleMenuService {
    @Resource
    private SysMenuService sysMenuService;
    @Resource
    private SysRoleMenuMapper sysRoleMenuMapper;
    /**
     * 根据角色id查询拥有的菜单 并且查询所有的菜单
     * @param roleId
     * @return
     */
    @Override
    public Map<String, Object> findSysRoleMenuByRoleId(Long roleId) {
        // 1. 查询所有菜单并且封装为树形结构
        List<SysMenu> allSysMenus = sysMenuService.findNodes();
        // 2. 根据角色id查询该角色拥有的菜单的id集合
        List<Long> idsByRoleId = sysRoleMenuMapper.findMenuIdsByRoleId(roleId);
        // 3. 封装返回值
        HashMap<String, Object> map = new HashMap<>();
        map.put("sysMenuList",allSysMenus);
        map.put("roleMenuIds",idsByRoleId);
        return map;
    }

    /**
     * 给角色分配菜单
     * @param assginMenuDto
     */
    @Override
    public void doAssign(AssginMenuDto assginMenuDto) {
        // 1. 删除指定角色原来的菜单
        sysRoleMenuMapper.deleteByRoleId(assginMenuDto.getRoleId());
        // 2. 添加角色菜单关系
        sysRoleMenuMapper.addBath(assginMenuDto);

    }
}
